local skel = fk.CreateSkill {
  name = "emo__zaohuo",
}

Fk:loadTranslationTable{
  ["emo__zaohuo"] = "造火",
  [":emo__zaohuo"] = "每回合各限一次：①你可以将两张牌当火【杀】使用；②你使用伤害牌结算后，可以将底牌依次当【火攻】使用；③你的伤害牌指定唯一目标后，可以随机扣置其手牌至剩一张，回合结束其获得之。",

  ["#emo__zaohuo-slash"] = "造火：你可以将2张牌当火【杀】使用",
  ["#emo__zaohuo-fire_attack"] = "造火：你可以将底牌依次当【火攻】使用",
  ["#emo__zaohuo-put"] = "造火：你可以扣置 %src 的手牌至只剩一张",
  ["#emo__zaohuo"] = "造火",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  prompt = "#emo__zaohuo-slash",
  pattern = "slash",
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2
  end,
  view_as = function (self, player, cards)
    if #cards ~= 2 then return nil end
    local c = Fk:cloneCard("fire__slash")
    c.skillName = skel.name
    c:addSubcards(cards)
    return c
  end,
  enabled_at_play = function(self, player)
    return player:usedEffectTimes(self.name) == 0
  end,
  enabled_at_response = function (self, player, response)
    return player:usedEffectTimes(self.name) == 0 and not response
  end,
})

skel:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.card.is_damage_card and player:usedEffectTimes(self.name) == 0 then
      return table.find(Card:getIdList(data.card), function (id)
        return player.room:getCardArea(id) == Card.Processing
      end) ~= nil
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = table.filter(Card:getIdList(data.card), function (id)
      return player.room:getCardArea(id) == Card.Processing
    end)
    if #cards == 0 then return false end
    local use = player.room:askToUseVirtualCard(player, {
      name = "fire_attack", skill_name = skel.name, prompt = "#emo__zaohuo-fire_attack", skip = true,
      card_filter = { n = {1, 1}, cards = cards },
    })
    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self)
    local cards = table.filter(Card:getIdList(data.card), function (id)
      return room:getCardArea(id) == Card.Processing
    end)
    while player:isAlive() and use do
      table.removeOne(cards, use.card:getEffectiveId())
      room:useCard(use)
      cards = table.filter(Card:getIdList(data.card), function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if #cards == 0 or player.dead then break end
      use = player.room:askToUseVirtualCard(player, {
        name = "fire_attack", skill_name = skel.name, prompt = "#emo__zaohuo-fire_attack", skip = true,
        card_filter = { n = {1, 1}, cards = cards },
      })
    end
  end,
})

skel:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and data.card.is_damage_card and player:usedEffectTimes(self.name) == 0 then
      local to = data.to
      return not to.dead and to:getHandcardNum() > 1
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name,
    prompt = "#emo__zaohuo-put:"..data.to.id })
  end,
  on_use = function (self, event, target, player, data)
    local cards = table.simpleClone(data.to:getCardIds("h"))
    table.remove(cards, math.random(#cards))
    data.to:addToPile("#emo__zaohuo", cards, false, skel.name)
  end,
})

skel:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return #player:getPile("#emo__zaohuo") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:obtainCard(player, player:getPile("#emo__zaohuo"), false, fk.ReasonPrey, player, skel.name)
  end,
})

return skel
